package com.chixing.controller;

import com.chixing.mapper.CustomerMapper;
import com.chixing.service.CustomerService;
import com.chixing.service.TravelNotesService;
import com.chixing.util.KindEditorResult;
import com.chixing.util.UploadImgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/image")
public class ImgUplaodController {
    @Autowired
    private TravelNotesService travelNotesService;

    @RequestMapping("/uploadImg")
    public @ResponseBody
    KindEditorResult uploadImg(HttpServletRequest request,
                               @RequestParam("imgFile") MultipartFile imgFile) {
        KindEditorResult result = new KindEditorResult();

        //取得原文件名字
        String fileName = imgFile.getOriginalFilename();
        //取得文件扩展名
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        //提取系统时间作为新文件名
        String prefix = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date().getTime());
        //保存路径
        // 上传文件名
        String filename = prefix+suffix;
        System.out.println("11"+filename);
        String imgUrl=null;
        try {
            imgUrl = UploadImgUtil.upload(imgFile.getBytes(),imgFile.getOriginalFilename());
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(imgUrl);
        try {
//            // 获取绝对路径
//            String realpath = "F:\\PracticalTraining\\workspace\\ziyouxing\\src\\main\\resources\\static\\images\\attached\\";
//            System.out.println(realpath);
//            // 上传文件
//            imgFile.transferTo(new File(realpath, Objects.requireNonNull(imgFile.getOriginalFilename())));
            result.setError(0);
            result.setUrl("http://rbtgr2byj.hn-bkt.clouddn.com/"+imgUrl);

            request.setAttribute("result", result);
        } catch (Exception e) {
            result.setError(1);
            result.setMessage("上传图片出错！");
        }
        System.out.println(result);
        return result;
    }


//        @PostMapping("/upload")
//        @ResponseBody
//        public String upload(MultipartFile fileUpload) {
//            String imgUrl="1";
//            try {
//                imgUrl = UploadImgUtil.upload(fileUpload.getBytes(),fileUpload.getOriginalFilename());
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//            return imgUrl;
//        }

    @ResponseBody
    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public Map<String, Object> uploadFile(HttpServletRequest servletRequest, @RequestParam("file") MultipartFile file)
            throws IOException {
        // 如果文件内容不为空，则写入上传路径
        if (!file.isEmpty()) {
            //取得原文件名字
            String fileName = file.getOriginalFilename();
            //取得文件扩展名
            String suffix = fileName.substring(fileName.lastIndexOf("."));
            //提取系统时间作为新文件名
            String prefix = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date().getTime());
            //保存路径
            // 上传文件名
            String filename = prefix+suffix;
            System.out.println("11"+filename);
            String imgUrl=null;
            try {
                imgUrl = UploadImgUtil.upload(file.getBytes(),file.getOriginalFilename());
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println(imgUrl);

            Map<String, Object> res = new HashMap<>();
            // 返回的是一个url对象,图片名称
            res.put("url", imgUrl);
            return res;
        } else {
            return null;
        }
    }


}
